#Macro

#Declare compiler

CC=nvcc

FLAG=$(LIB) 

CERTI_HOME=./certi_home

CERTI_LIB=-I $(CERTI_HOME)/include  -L $(CERTI_HOME)/lib/ -lRTI-NGd -lFedTimed -lHLAd -DRTI_USES_STD_FSTREAM 

XLIB_LIB=-I /usr/include/X11/ -L /usr/X11/lib 

LIB=-I ./lib -L .

#Variables

CA_PATTERN_RIVER_FED=vonneumann
CA_PATTERN_SENSOR_NET_FED=vonneumann
CA_PATTERN_FOREST_FED=vonneumann
CA_PATTERN_FRONTIER=vonneumann

FILE_NAME_RIVER_NET=networkRiver.cu
FILE_NAME_SENSOR_NET=networkSensorNet.cu
FILE_NAME_FOREST_NET=networkForest.cu
FILE_NAME_FRONTIER_NET=networkFrontier.cu

DATA_FILE_NAME_SENSOR_NET=dataSensorNet.cu

#Directories

SOURCE_DIR=.

FEDERATES_DIR=Federates

RIVER_FEDERATE_DIR=RiverFed

NETWORK_FEDERATE_DIR=NetworkFed

FOREST_FEDERATE_DIR=ForestFed

WINDOW_FEDERATE_DIR=WindowFed

#Target

all: clean generate cellRiver  Makefile 

#target without file
.PHONY: clean generate federation

pipeline: pipeline.cu funcCA.o funcView.o
	$(CC) $(FLAG) $(XLIB_LIB) pipeline.cu funcCA.o funcView.o -lX11 -o pipeline

cellRiver2: cellRiver2.cu funcCA.o funcView.o
	$(CC) $(FLAG) $(XLIB_LIB) cellRiver2.cu funcCA.o funcView.o -lX11 -o cellRiver2

imageProcessing.o: imageProcessing.cu
	$(CC) $(FLAG) -c imageProcessing.cu -o imageProcessing.o 

imageProcessing: imageProcessing.o funcImageProcessing.o funcNetwork.o funcCommon.o
	$(CC) $(FLAG) imageProcessing.o funcImageProcessing.o funcNetwork.o funcCommon.o -o imageProcessing 

WindowFederate.o: $(WINDOW_FEDERATE_DIR)/WindowFederate.cu 
	@echo "Building the target WindowFederate.o ..."
	$(CC) $(CERTI_LIB) $(XLIB_LIB) $(LIB) -c $(WINDOW_FEDERATE_DIR)/WindowFederate.cu -o $(WINDOW_FEDERATE_DIR)/WindowFederate.o

WindowFederate: WindowFederate.o funcCA.o funcView.o funcNetwork.o funcCommon.o
	@echo "-- Building target ForestFederate ..."
	$(CC) $(CERTI_LIB) -lX11 $(WINDOW_FEDERATE_DIR)/WindowFederate.o funcCA.o funcView.o funcNetwork.o funcCommon.o -o WindowFederate

ForestFederate.o: $(FOREST_FEDERATE_DIR)/ForestFederate.cu genNetworkForest 
	@echo "Building the target ForestFederate.o ..."
	./$(FOREST_FEDERATE_DIR)/genNetworkForest $(FILE_NAME_FOREST_NET)  $(CA_PATTERN_FOREST_FED)
	mv $(FILE_NAME_FOREST_NET) $(FOREST_FEDERATE_DIR)
	$(CC) $(CERTI_LIB) $(XLIB_LIB) $(LIB) -c $(FOREST_FEDERATE_DIR)/ForestFederate.cu -o $(FOREST_FEDERATE_DIR)/ForestFederate.o

ForestFederate: ForestFederate.o funcCA.o funcView.o funcImageProcessing.o funcNetwork.o funcCommon.o
	@echo "-- Building target ForestFederate ..."
	$(CC) $(CERTI_LIB) -lX11 $(FOREST_FEDERATE_DIR)/ForestFederate.o funcCA.o funcView.o funcImageProcessing.o funcNetwork.o funcCommon.o -o ForestFederate

NetworkFederate.o: $(NETWORK_FEDERATE_DIR)/NetworkFederate.cu genDataSensorNet
	@echo "Building the target NetworkFederate.o ..."
	#Creating dataSensorNet.cu for NetworkFederate.cu target.
	#./$(NETWORK_FEDERATE_DIR)/genDataSensorNet $(DATA_FILE_NAME_SENSOR_NET)
	#mv $(DATA_FILE_NAME_SENSOR_NET) $(NETWORK_FEDERATE_DIR)
	$(CC) $(CERTI_LIB) $(XLIB_LIB) $(LIB) -c $(NETWORK_FEDERATE_DIR)/NetworkFederate.cu -o $(NETWORK_FEDERATE_DIR)/NetworkFederate.o

NetworkFederate: NetworkFederate.o funcCA.o funcView.o funcImageProcessing.o funcNetwork.o funcCommon.o
	@echo "Building target NetworkFederate ..."
	$(CC) $(CERTI_LIB) -lX11 $(NETWORK_FEDERATE_DIR)/NetworkFederate.o funcCA.o funcView.o funcImageProcessing.o funcNetwork.o funcCommon.o -o NetworkFederate

RiverFederate.o: $(RIVER_FEDERATE_DIR)/RiverFederate.cu genNetworkRiver 
	@echo "Building the target RiverFederate.o ..."
	./$(RIVER_FEDERATE_DIR)/genNetworkRiver $(FILE_NAME_RIVER_NET)  $(CA_PATTERN_RIVER_FED)
	mv $(FILE_NAME_RIVER_NET) $(RIVER_FEDERATE_DIR)
	$(CC) $(CERTI_LIB) $(XLIB_LIB) $(LIB) -c $(RIVER_FEDERATE_DIR)/RiverFederate.cu -o $(RIVER_FEDERATE_DIR)/RiverFederate.o

RiverFederate: RiverFederate.o funcCA.o funcView.o funcNetwork.o funcImageProcessing.o funcCommon.o funcFederation.o
	@echo "-- Building target RiverFederate ..."
	$(CC) $(CERTI_LIB) -lX11 $(RIVER_FEDERATE_DIR)/RiverFederate.o funcCA.o funcView.o funcNetwork.o funcCommon.o funcImageProcessing.o funcFederation.o -o RiverFederate

federation: RiverFederate NetworkFederate ForestFederate WindowFederate

funcFederation.o: funcFederation.cu  
	$(CC) $(FLAG) -c funcFederation.cu -o funcFederation.o -arch=sm_20

funcImageProcessing.o: funcImageProcessing.cu  
	$(CC) $(FLAG) -c funcImageProcessing.cu -o funcImageProcessing.o -arch=sm_20

funcCA.o: funcCA.cu  
	$(CC) $(FLAG) -c funcCA.cu -o funcCA.o -arch=sm_20

funcCommon.o: funcCommon.cu 
	$(CC) $(FLAG) -c funcCommon.cu -o funcCommon.o -arch=sm_20

funcRouting.o: funcRouting.cu 
	$(CC) $(FLAG) -c funcRouting.cu -o funcRouting.o -arch=sm_20

funcNetwork.o: funcNetwork.cu 
	$(CC) $(FLAG) -c funcNetwork.cu -o funcNetwork.o -arch=sm_20

funcView.o: funcView.cu 
	$(CC) $(FLAG) $(XLIB_LIB) -c funcView.cu -o funcView.o -arch=sm_20

genNetwork: generate.cu funcNetwork.o funcCommon.o
	@echo "Removing file genNetwork ..."
	rm -f genNetwork
	$(CC) $(LIB) generate.cu funcNetwork.o funcCommon.o -o genNetwork -arch=sm_20

genNetworkRiver: $(RIVER_FEDERATE_DIR)/generateRiver.cu funcNetwork.o funcCommon.o
	@echo "Removing generator genNetworkRiver ..."
	rm -f $(RIVER_FEDERATE_DIR)/genNetworkRiver
	$(CC) $(LIB) $(RIVER_FEDERATE_DIR)/generateRiver.cu funcNetwork.o funcCommon.o -o $(RIVER_FEDERATE_DIR)/genNetworkRiver -arch=sm_20

genNetworkFrontier: $(NETWORK_FEDERATE_DIR)/generateFrontier.cu funcNetwork.o funcCommon.o
	@echo "Removing generator genFrontier ..."
	rm -f $(NETWORK_FEDERATE_DIR)/genNetworkFrontier
	$(CC) $(LIB) $(NETWORK_FEDERATE_DIR)/generateFrontier.cu funcNetwork.o funcCommon.o -o $(NETWORK_FEDERATE_DIR)/genNetworkFrontier -arch=sm_20

genNetworkForest: $(FOREST_FEDERATE_DIR)/generateForest.cu funcNetwork.o funcCommon.o
	@echo "Removing generator genNetworkForest ..."
	rm -f $(FOREST_FEDERATE_DIR)/genNetworkForest
	$(CC) $(LIB) $(FOREST_FEDERATE_DIR)/generateForest.cu funcNetwork.o funcCommon.o -o $(FOREST_FEDERATE_DIR)/genNetworkForest -arch=sm_20

genDataSensorNet.o: $(NETWORK_FEDERATE_DIR)/generateSensorNet.cu genNetworkFrontier
	@echo "Building the target genSensorNet.o ..."
	./$(NETWORK_FEDERATE_DIR)/genNetworkFrontier $(FILE_NAME_FRONTIER_NET)  $(CA_PATTERN_FRONTIER)
	mv $(FILE_NAME_FRONTIER_NET) $(NETWORK_FEDERATE_DIR)
	$(CC) $(XLIB_LIB) $(LIB) -c $(NETWORK_FEDERATE_DIR)/generateSensorNet.cu -o $(NETWORK_FEDERATE_DIR)/genDataSensorNet.o

genDataSensorNet: genDataSensorNet.o funcCA.o funcView.o funcRouting.o
	@echo "-- Building target genDataSensorNet ..."
	rm -f $(NETWORK_FEDERATE_DIR)/genDataSensorNet
	$(CC) -lX11 $(NETWORK_FEDERATE_DIR)/genDataSensorNet.o funcCA.o funcView.o funcRouting.o -o $(NETWORK_FEDERATE_DIR)/genDataSensorNet	

generate: genNetwork
	@echo "Removing network.cu"
	rm -f network.cu
	@echo "Generating cell network!"
	./genNetwork

cellNetwork.o: cellNetwork.cu  
	$(CC) $(FLAG) $(XLIB_LIB) -c cellNetwork.cu -o cellNetwork.o -arch=sm_20

cellNetwork: funcCA.o funcRouting.o cellNetwork.o funcView.o
	$(CC) $(FLAG) funcCA.o funcRouting.o funcView.o cellNetwork.o -o cellNetwork -lX11 -arch=sm_20

cellCricket.o: cellCricket.cu cellNetwork.h 
	$(CC) $(FLAG) $(XLIB_LIB) -c cellCricket.cu -o cellCricket.o

cellCricket: funcCA.o funcRouting.o funcView.o cellCricket.o 
	$(CC)  funcCA.o funcRouting.o cellCricket.o funcView.o -o cellCricket -lX11 -arch=sm_20

cellRiver.o: cellRiver.cu 
	$(CC) $(FLAG) $(XLIB_LIB) -G -c cellRiver.cu -o cellRiver.o -arch=sm_20

cellRiver: cellRiver.o  funcCA.o funcView.o 
	$(CC)  funcCA.o cellRiver.o funcView.o -o cellRiver -lX11 -arch=sm_20

	@echo "-- BUILD COMPLETED!"

clean:
	@echo "Cleaning project before compiling ..."
	rm -f *.o *~ *.dot cellNetwork cellRiver cellNetwork.zip cellNetwork.tar genNetwork $(RIVER_FEDERATE_DIR)/*.o $(NETWORK_FEDERATE_DIR)/*.o $(FOREST_FEDERATE_DIR)/*.o RiverFederate ForestFederate $()/NetworkFederate WindowFederate

zip:
	rm -f cellNetwork.zip cellNetwork *.o *~
	zip cellNetwork.zip data* network* cell* func* Makefile
tar: 	
	rm -f cellNetwork.tar cellNetwork *.o *~
	tar cfv cellNetwork.tar data* network* cell* func* Makefile
doc:
	rm -r Doc
	doxygen Doxyfile


